package client;

import provider.*;
import tools.FileoutputUtil;
import tools.StringUtil;

import java.util.*;

public class SkillFactory {
    private static Map<Integer, ISkill> skills;
    private static Map<Integer, List<Integer>> summonSkills;
    private static Map<Integer, SummonSkillEntry> summonSkillsInfo;
    private static MapleData stringSkills;

    public static void LoadSkillInformaion() {
        System.out.println("[" + FileoutputUtil.CurrentReadable_Time() + "][信息]:初始化技能信息列表");
        MapleDataProvider skillWZ = MapleDataProviderFactory.getDataProvider("Skill.wz");
        MapleDataDirectoryEntry root = skillWZ.getRoot();
        for (MapleDataFileEntry topDir : root.getFiles()) {
            if (topDir.getName().length() <= 8) {
                for (MapleData data : skillWZ.getData(topDir.getName())) {
                    if (data.getName().equals("skill")) {
                        for (MapleData subData : data) {
                            if (subData != null) {
                                int skillId = Integer.parseInt(subData.getName());
                                Skill skil = Skill.loadFromData(skillId, subData);
                                List<Integer> job = SkillFactory.summonSkills.get(Integer.valueOf(skillId / 10000));
                                if (job == null) {
                                    job = (List<Integer>) new ArrayList();
                                    SkillFactory.summonSkills.put(Integer.valueOf(skillId / 10000), job);
                                }
                                job.add(Integer.valueOf(skillId));
                                skil.setName(getName(skillId));
                                SkillFactory.skills.put(Integer.valueOf(skillId), skil);
                                MapleData summon_data = subData.getChildByPath("summon/attack1/info");
                                if (summon_data == null) {
                                    continue;
                                }
                                SummonSkillEntry sse = new SummonSkillEntry();
                                sse.attackAfter = (short) MapleDataTool.getInt("attackAfter", summon_data, 999999);
                                sse.type = (byte) MapleDataTool.getInt("type", summon_data, 0);
                                sse.mobCount = (byte) MapleDataTool.getInt("mobCount", summon_data, 1);
                                SkillFactory.summonSkillsInfo.put(Integer.valueOf(skillId), sse);
                            }
                        }
                    }
                }
            }
        }
    }

    public static ISkill getSkill(int id) {
        if (!SkillFactory.skills.isEmpty()) {
            return (ISkill) SkillFactory.skills.get(Integer.valueOf(id));
        }
        return null;
    }

    public static long getDefaultSExpiry(ISkill skill) {
        if (skill == null) {
            return -1L;
        }
        return skill.isTimeLimited() ? (System.currentTimeMillis() + 2592000000L) : -1L;
    }

    public static List<Integer> getSkillsByJob(int jobId) {
        return SkillFactory.summonSkills.get(Integer.valueOf(jobId));
    }

    public static String getSkillName(int id) {
        ISkill skil = getSkill(id);
        if (skil != null) {
            return skil.getName();
        }
        return "";
    }

    public static String getName(int id) {
        String strId = Integer.toString(id);
        strId = StringUtil.getLeftPaddedStr(strId, '0', 7);
        MapleData skillroot = SkillFactory.stringSkills.getChildByPath(strId);
        if (skillroot != null) {
            return MapleDataTool.getString(skillroot.getChildByPath("name"), "");
        }
        return "";
    }

    public static SummonSkillEntry getSummonData(int skillid) {
        return (SummonSkillEntry) SkillFactory.summonSkillsInfo.get(Integer.valueOf(skillid));
    }

    public static Collection<ISkill> getAllSkills() {
        return SkillFactory.skills.values();
    }

    static {
        skills = new HashMap();
        summonSkills = new HashMap();
        summonSkillsInfo = new HashMap();
        stringSkills = MapleDataProviderFactory.getDataProvider("String.wz").getData("Skill.img");
    }
}
